<?php
/**
 * 教室分类
 */
namespace app\admin\controller;

use think\App;
use think\facade\View;
use app\admin\model\Classgrade as grade;
use app\admin\validate\Classroom as validate;

class Classgrade extends Common{

    public function __construct(App $app)
    {
        $this->validate = validate::class;
        $this->localModel = new grade();
        parent::__construct($app);
    }

    public function index()
    {
        $where = [];
        $name = $this->request->get('name', '');
        $status = $this->request->get('status', 0);

        if($name)  $where[] = ['name', 'like', "%$name%"];
        if($status)  $where[] = ['status', '=', $status];

        $res = $this->localModel->findAllPageInfo('*', $where, ['id'=>'desc'], $this->request->get());
        View::assign(['gradeData'=>$res['data'], 'pageData'=>$res['page'], 'name'=>$name, 'status'=>$status]);
        return view();
    }

    public function add(){
        $id = $this->request->get('id', 0);

        if($id){
            $gradeInfo = $this->localModel->findDetaiInfoById('*', $id);
            View::assign(['gradeInfo'=>$gradeInfo, 'id'=>$id]);
            return view('edit');
        }else{
            return view('add');
        }
    }

    //保存
    public function save(){
        $dataArr = $this->request->request('grade/a', []);
        $id = $this->request->post('id', 0);

        if($msg = $this->checkData($dataArr, 'grade'))  return json(['result'=>'fail', 'title'=>$msg]);
        $res = $this->localModel->saveInfo($dataArr, $id);

        if(!$res)  return json(['result'=>'fail', 'title'=>'操作失败！']);

        event('ChangeRoleColumn');
        $this->logsData = ['cid'=>$id ? $id : $res, 'action'=>$id ? 'edit' : 'add', 'msg'=>$dataArr['name']];
        return json(['result'=>'success', 'title'=>'恭喜您操作成功！', 'url'=>(string)url('Classgrade/index')]);
    }



}
